﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using System.Collections;
using BBMS.DLL.Base;
using ExcelLibrary.SpreadSheet;

namespace BBMS.DLL.Forms.Catalog.User
{
    public partial class frmUserFilter : FrmFilterBase
    {

        public frmUserFilter()
        {
            InitializeComponent();
            GetUserNameList();
        }

        private void GetUserNameList()
        {
            ArrayList arret = NhanVienList.getAllUserName();
            cbxUserNameList.Items.Add(new KeyValuePair("", "Tất cả"));
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    KeyValuePair kvp = (KeyValuePair)arret[i];
                    cbxUserNameList.Items.Add(kvp);
                }
            }
            cbxUserNameList.SelectedIndex = 0;
        }


        public new static readonly string FunctionKey = "DMNhanVien";
        protected override string GetFunctionKey() { return "DMNhanVien"; }
        protected override BBMS.DLL.Base.ObjectBase GetObjectBase() { return new NhanVien(); }

        protected override void GenColHeaders()
        {
            lvList.Columns.Clear();
            lvList.Columns.Add("Mã nhân viên", 50);
            lvList.Columns.Add("Tên đăng nhập", 150);
            lvList.Columns.Add("Tên đầy đủ", 150);
            lvList.Columns.Add("Ngày sinh", 80);
            //lvList.Columns.Add("Mật khẩu", 0);
            lvList.Columns.Add("Số ĐT", 100);
            lvList.Columns.Add("ĐTDĐ", 100);
            lvList.Columns.Add("Email", 100);
            lvList.Columns.Add("Bộ phận", 100);
            lvList.Columns.Add("Chức vụ", 100);

        }
        protected override ArrayList GenSampleData()
        {
            ArrayList ar = new ArrayList();
            ar.Add(new string[] { "STT", "Tên đăng nhập", "Tên đầy đủ", "Ngày sinh", "Mật khẩu",
            "Số ĐT", "ĐTDĐ",  "Bộ phận", "Chức vụ","Email"});
            NhanVien nhanvienList = new NhanVien();
            ArrayList arr = nhanvienList.GetAllObject(fParams);
            if (arr != null)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    NhanVien nv = (NhanVien)arr[i];
                    ar.Add(new string[] { nv.ID.ToString(), nv.UserName, nv.FullName, nv.Birthday.ToString(), nv.Password
                    , nv.Phone, nv.Mobile, nv.Department, nv.Position, nv.Email});
                }
            }
            //ar.Add(new string[] { "QTP", "Quầy thực phẩm", "Food counter", "NTP" });
            // define col width
            //ar.Add(new int[] { 100, 300, 100, 300 });
            return ar;
        }

        protected override ArrayList GetImportedList(Worksheet sheet)
        {
            ArrayList arret = new ArrayList();
            for (int i = 1; i < sheet.Cells.Rows.Count; i++)
            {
                NhanVien nv = new NhanVien();
                Cell cell = sheet.Cells[i, 0];
                nv.ID = Convert.ToInt32(cell.Value.ToString());
                cell = sheet.Cells[i, 1];
                nv.UserName = cell.Value.ToString();
                cell = sheet.Cells[i, 2];
                nv.FullName = cell.Value.ToString();
                cell = sheet.Cells[i, 3];
                if (cell.Value.ToString()!="") nv.Birthday = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 4];
                nv.Password = cell.Value.ToString();
                cell = sheet.Cells[i, 5];
                nv.Phone = cell.Value.ToString();
                cell = sheet.Cells[i, 6];
                nv.Mobile = cell.Value.ToString();
                cell = sheet.Cells[i, 7];
                nv.Department = cell.Value.ToString();
                cell = sheet.Cells[i, 8];
                nv.Position = cell.Value.ToString();
                cell = sheet.Cells[i, 9];
                nv.Email = cell.Value.ToString();
                arret.Add(nv);

            }
            return arret;
        }

        protected override void UpdateFilterParams()
        {
            if (txtFullName.Text.Trim() != "" || cbxUserNameList.SelectedIndex != -1)
            {
                fParams = new FilterParams();
                if (txtFullName.Text.Trim() != "")
                {
                    FilterCondition fcFullName = new FilterCondition("FullName", "FullName", "%" + txtFullName.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcFullName);
                }

                if (cbxUserNameList.SelectedIndex != 0)
                {
                    FilterCondition fcUserName = new FilterCondition("UserName", "UserName", ((KeyValuePair)cbxUserNameList.SelectedItem).Value, SQLOperator.EQUAL);
                    fParams.ArParams.Add(fcUserName);
                }
                fParams.CurrentPage = 0;
            }
            else
            {
                MessageBox.Show(null, "Bạn phải nhập nội dung tìm kiếm", "Thông báo");
            }
        }

        protected override FrmEditBase CreateEditForm(BBMS.DLL.Base.ObjectBase obj)
        {
            return new frmUserUpdate(obj);
        }

        protected override void AddNewObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            if (obj == null) return;
            ListViewItem it = new ListViewItem(((NhanVien)obj).ID.ToString());
            it.SubItems.Add(((NhanVien)obj).UserName);
            it.SubItems.Add(((NhanVien)obj).FullName);
            if (((NhanVien)obj).Birthday != null)
                it.SubItems.Add(((DateTime)((NhanVien)obj).Birthday).ToString("dd/MM/yyyy"));
            else it.SubItems.Add("");
            it.SubItems.Add(((NhanVien)obj).Phone);
            it.SubItems.Add(((NhanVien)obj).Mobile);
            it.SubItems.Add(((NhanVien)obj).Email);
            it.SubItems.Add(((NhanVien)obj).Department);
            it.SubItems.Add(((NhanVien)obj).Position);
            it.Tag = obj;
            lvList.Items.Add(it);
        }

        protected override void UpdateObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            foreach (ListViewItem it in lvList.Items)
            {
                if (((NhanVien)it.Tag).ID == ((NhanVien)obj).ID)
                {
                    it.SubItems[1].Text = ((NhanVien)obj).UserName;
                    it.SubItems[2].Text = ((NhanVien)obj).FullName;
                    if (((NhanVien)obj).Birthday != null)
                    it.SubItems[3].Text = ((DateTime)((NhanVien)obj).Birthday).ToString("dd/MM/yyyy");
                    else it.SubItems[3].Text = "";
                    it.SubItems[4].Text = ((NhanVien)obj).Phone;
                    it.SubItems[5].Text = ((NhanVien)obj).Mobile;
                    it.SubItems[6].Text = ((NhanVien)obj).Email;
                    it.SubItems[7].Text = ((NhanVien)obj).Department;
                    it.SubItems[8].Text = ((NhanVien)obj).Position;
                    it.Tag = obj;
                    break;
                }
            }
        }


    }
}
